package com.fps.web.controller;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fps.web.model.BaseUser;
import com.fps.web.service.BaseUserService;
import com.fps.web.service.BasememoryService;
import com.fps.web.service.BasemenuService;
import com.fpswork.common.Constant;
import com.fpswork.core.model.PageEntity;


/**
 * 登录页面
    * @date 2015-11-19
    *
 */
@Controller
public class LoginController {
    
    @Autowired
    BaseUserService baseUserService;
    @Autowired
    BasemenuService basemenuService;
    @Autowired
    BasememoryService basememoryService;
    
   
    
    
    @RequestMapping("/login")
    public ModelAndView testInDb(){
    	ModelAndView modelAndView = new ModelAndView();    	
		modelAndView.setViewName("login");      
        return modelAndView;
        
    }
    @RequestMapping(value = "/loginSubmit", method = RequestMethod.POST)
	public @ResponseBody Map<String, String> loginsubmit(@ModelAttribute("BaseUser") BaseUser baseUser,
			HttpServletRequest request)  throws Exception{    	
		Map<String, String> map = null;
			map = baseUserService.loginCheck(baseUser,request);
		return map;
	}
    /**
	 * 显示主页界面
	 */
	@RequestMapping("/home")
	public ModelAndView home(HttpServletRequest request, HttpServletResponse response, String mid) throws Exception{
		ModelAndView modelAndView = new ModelAndView();
		PageEntity pageEntity = new PageEntity();
		basemenuService.queryBasemenuList(pageEntity);
		modelAndView.addObject("baseMenu",pageEntity.getDatalist());
		modelAndView.setViewName("home");		
		return modelAndView;
	}
	
    /**
	 * 首页
	 */
	@RequestMapping("/homepage")
	public ModelAndView homepage(PageEntity pageEntity,HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView modelAndView = new ModelAndView();
		Map<String, String> map = new HashMap<String, String>();
		BaseUser user = (BaseUser) request.getSession().getAttribute(Constant.SESSION_USER_KEY);
    	map.put("createuserid", user.getId());
    	pageEntity.setSearch(map);
		basememoryService.queryBasememoryList(pageEntity);
		modelAndView.addObject("baseMemory",pageEntity.getDatalist());
		modelAndView.setViewName("homepage");		
		return modelAndView;
	}	
	    

}
